package com.yunji.news.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunji.news.common.Result;
import com.yunji.news.entity.Major;
import com.yunji.news.service.MajorService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 专业管理控制器
 *
 * @author yunji
 * @since 2024-11-25
 */
@RestController
@RequestMapping("/admin/majors")
@RequiredArgsConstructor
public class AdminMajorController {

    private final MajorService majorService;

    /**
     * 分页查询专业列表
     */
    @GetMapping
    public Result<Page<Major>> getMajorPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Long schoolId,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String category) {
        
        Page<Major> majorPage = new Page<>(page, size);
        Page<Major> result = majorService.getMajorPage(majorPage, schoolId, name, category);
        return Result.success(result);
    }

    /**
     * 获取专业详情
     */
    @GetMapping("/{id}")
    public Result<Major> getMajorDetail(@PathVariable Long id) {
        Major major = majorService.getById(id);
        if (major == null) {
            return Result.error("专业不存在");
        }
        return Result.success(major);
    }

    /**
     * 创建专业
     */
    @PostMapping
    public Result<String> createMajor(@Valid @RequestBody Major major) {
        boolean success = majorService.createMajor(major);
        if (success) {
            return Result.success("创建成功");
        }
        return Result.error("创建失败");
    }

    /**
     * 更新专业
     */
    @PutMapping("/{id}")
    public Result<String> updateMajor(@PathVariable Long id, @Valid @RequestBody Major major) {
        major.setId(id);
        boolean success = majorService.updateMajor(major);
        if (success) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    /**
     * 删除专业
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteMajor(@PathVariable Long id) {
        boolean success = majorService.deleteMajor(id);
        if (success) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    /**
     * 批量删除专业
     */
    @DeleteMapping("/batch")
    public Result<String> deleteMajorBatch(@RequestBody List<Long> ids) {
        boolean success = majorService.deleteMajorBatch(ids);
        if (success) {
            return Result.success("批量删除成功");
        }
        return Result.error("批量删除失败");
    }

    /**
     * 获取专业分类列表
     */
    @GetMapping("/categories")
    public Result<List<String>> getCategories() {
        List<String> categories = majorService.getCategories();
        return Result.success(categories);
    }

    /**
     * 根据院校ID获取专业列表
     */
    @GetMapping("/school/{schoolId}")
    public Result<List<Major>> getMajorsBySchoolId(@PathVariable Long schoolId) {
        List<Major> majors = majorService.getMajorsBySchoolId(schoolId);
        return Result.success(majors);
    }
}